package iteratormodule;

import java.util.*;

/**
 * @author by 何坤
 * @Classname TestIterator01
 * @Description TODO
 * @Date 2019/10/17 10:58
 */
public class TestIterator01 {
    public static void main(String[] args) {
        testIteratorList();
        System.out.println("==================================");
        testIteratorSet();
        System.out.println("==================================");
        testIteratorMap();
    }

    private static void testIteratorList(){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        for (Iterator<String> iterator = list.iterator(); iterator.hasNext();){
            System.out.println(iterator.next());
        }
    }

    private static void testIteratorSet(){
        Set<String> set = new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");
        set.add("d");
        for (Iterator<String> iterator = set.iterator(); iterator.hasNext();){
            System.out.println(iterator.next());
        }
    }

    private static void testIteratorMap(){
        Map<Integer, Object> map = new HashMap<>();
        map.put(1, "春");
        map.put(2, "夏");
        map.put(3, "秋");
        map.put(4, "冬");
        //方式1：遍历entrySet()
        Set<Map.Entry<Integer, Object>> entrySet = map.entrySet();
        for (Iterator<Map.Entry<Integer, Object>> entryIterator = entrySet.iterator(); entryIterator.hasNext();){
            System.out.println(entryIterator.next().getValue());
        }
        System.out.println("===========================");
        //方式2：遍历keySet()
        Set<Integer> keySet = map.keySet();
        for (Iterator<Integer> integerIterator = keySet.iterator(); integerIterator.hasNext();){
            System.out.println(map.get(integerIterator.next()));
        }
    }
}
